﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iOak.Business.Resumes;
using iOak.Domain.Resumes;

namespace iOak.Web.Popups
{
    /// <summary>
    /// AttachementShow 的摘要说明
    /// </summary>
    public class AttachementShow : IHttpHandler
    {
        private static readonly AttachmentService itemAttachmentService = new AttachmentService();
        private static readonly CompanyService companyService = new CompanyService();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            // 设定缓存类型
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.BufferOutput = false;
            byte[] imageData = null;
            // 获取参数
            if (context.Request.QueryString["AttachmentId"] != null) 
            {
                string fileId = context.Request.QueryString["AttachmentId"] as string;

                if (fileId != null && !string.IsNullOrWhiteSpace(fileId))
                {
                    // 获取当前文件对象
                    var fileItem = itemAttachmentService.Retrieve(new Guid(fileId));
                    if (fileItem != null && fileItem.PictureBinary != null)
                    {
                        imageData = fileItem.PictureBinary;
                    }
                }
            }
            else if (context.Request.QueryString["CompanyId"] != null) 
            {
                Company company = companyService.Retrieve(Guid.Parse(context.Request.QueryString["CompanyId"]));
                if (company.BusinessLicense != null && company.BusinessLicense.Length > 0) 
                {
                    imageData = company.BusinessLicense;
                }
            }

            if (imageData != null)
            {
                context.Response.OutputStream.Write(imageData, 0, imageData.Count());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}